home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_005 / keyboard / keyboard.c < prev   
C/C++ Source or Header  |  1992-05-06  |  4KB  |  127 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. /* keyboard.c */
  31.  
  32. /* sample program to demonstrate direct communications with the keyboard,
  33.  * won't work unless input device is disabled, so that keyboard can
  34.  * be accessed individually.  (It will compile and it will run, but
  35.  * this program will get some of the keyboard's inputs,  and the input
  36.  * device will steal the rest... no guarantee that F1 Key can break it out.
  37.  *
  38.  * To try the program, if run under the AmigaDOS CLI, strike any key, then
  39.  * hit return.  (You won't see any responses until each return key... DOS
  40.  * is sitting on the input stream with its input editor as well as the
  41.  * input device.)  By rapidly hitting F1 then Return several times, 
  42.  * eventually you can generate a hex 50 that exits the program.  This
  43.  * program is provided for those who are taking over the machine.  It
  44.  * is not intended as a general purpose keyboard interface under DOS.
  45.  *
  46.  * Demo is more effective if you make the CLI window smaller, then
  47.  * click the select button in the (Workbench) space below the CLI
  48.  * window.  Then no console will be receiving the keystrokes and
  49.  * more of them will be reported to the startup CLI window.   Most
  50.  * users will leave the keyboard device attached to the input device
  51.  * and install a handler in the input stream instead of doing it
  52.  * this way.
  53.  *
  54.  * Author:  Rob Peck, 12/1/85
  55.  *
  56.  * This code may be freely utilized in the creation of programs for the Amiga. 
  57.  */
  58.  
  59. #include <exec/types.h>
  60. #include <exec/io.h>
  61. #include <exec/devices.h>
  62. #include <devices/keyboard.h>
  63. #include <devices/inputevent.h>
  64.  
  65. #define F1KEY 0x50
  66.  
  67. extern struct MsgPort *CreatePort();
  68. extern struct IOStdReq *CreateStdIO();
  69.  
  70. SHORT error;
  71.  
  72. struct IOStdReq *keyreq;
  73. struct MsgPort *keyport;
  74. struct InputEvent *keydata;    /* pointer into the returned data area
  75.                    where an input event has been sent */
  76. BYTE keybuffer[sizeof( struct InputEvent )];
  77.  
  78. main()
  79. {
  80.     keyport = CreatePort(0,0);
  81.     if(keyport == 0) { printf("\nError during CreatePort"); 
  82.                exit(-1); 
  83.              }        
  84.     keyreq = CreateStdIO(keyport);    
  85.         /* make an io request block for 
  86.          * communicating with the keyboard */
  87.     if(keyreq == 0) { printf("\nError during CreateStdIO");
  88.               DeletePort(keyport);
  89.               exit(-2); 
  90.             }
  91.     error = OpenDevice("keyboard.device",0,keyreq,0);
  92.             /* open the device for access */
  93.  
  94.     if (error != 0) { printf("\nCan't open keyboard!"); 
  95.               ReturnMemoryToSystem();
  96.               exit(-100); 
  97.             }
  98.     keyreq->io_Length = sizeof(struct InputEvent);    
  99.     /* read one event each time we go back to the keyboard */
  100.  
  101.     keyreq->io_Data = (APTR)keybuffer;    
  102.     /* show where to put the data when read */
  103.  
  104.     keydata = (struct InputEvent *)keybuffer;
  105.  
  106.     keyreq->io_Command = KBD_READEVENT;    /* get an event!! */
  107.  
  108.  
  109.     for(;;)        /* FOREVER */
  110.     {
  111.     printf("\n Ready to retrieve another key\n");
  112.     DoIO( keyreq );
  113.     if(keydata->ie_Code == F1KEY) break;    
  114.     printf("\n Raw key found this time was %lx",keydata->ie_Code);
  115.     }
  116.     printf("\nFINALLY found an F1 key!!!   Exiting...");
  117.     ReturnMemoryToSystem();    /* can't get here because of FOREVER,
  118.                  * but if user provides an exit..... */
  119. }        
  120.  
  121. ReturnMemoryToSystem()   
  122. {
  123.     DeleteStdIO(keyreq);
  124.     DeletePort(keyport);
  125.     return(0);
  126. }
  127.